//package com.light.netty.server.init;
//
//import com.light.netty.server.handler.HttpBusinessHandler;
//import com.light.netty.server.handler.TcpBusinessHandler;
//import io.netty.bootstrap.ServerBootstrap;
//import io.netty.buffer.PooledByteBufAllocator;
//import io.netty.channel.*;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioServerSocketChannel;
//import io.netty.handler.codec.http.HttpClientCodec;
//import io.netty.handler.codec.http.HttpObjectAggregator;
//import io.netty.handler.codec.http.HttpServerCodec;
//import io.netty.handler.codec.string.StringDecoder;
//import io.netty.handler.codec.string.StringEncoder;
//import io.netty.handler.logging.LogLevel;
//import io.netty.handler.logging.LoggingHandler;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//
///**
// * @author fuhui
// * @Description：
// * @data 2025/3/13 11:10
// */
//@Component
//public class HttpNettyServerInit {
//
//    private static final int PORT = 8081;
//    private static final int THREAD_POOL_SIZE = 4;
//
//    @PostConstruct
//    public void init() throws Exception {
//        System.out.println("初始化HTTP服务端...........");
//
//        new Thread(() -> {
//            // 1. 初始化线程组
//            EventLoopGroup bossGroup = new NioEventLoopGroup(1);
//            EventLoopGroup workerGroup = new NioEventLoopGroup(THREAD_POOL_SIZE);
//
//            try {
//                // 3. 配置 ServerBootstrap
//                ServerBootstrap b = new ServerBootstrap();
//                b.group(bossGroup, workerGroup)
//                        .channel(NioServerSocketChannel.class) // 使用 NIO 模式
//                        .childHandler(new ChannelInitializer<SocketChannel>() {
//                            @Override
//                            protected void initChannel(SocketChannel ch) throws Exception {
//                                ChannelPipeline pipeline = ch.pipeline();
//
//                                // 日志记录
//                                pipeline.addLast(new LoggingHandler(LogLevel.INFO));
//
//                                pipeline.addLast(new HttpServerCodec());
//                                pipeline.addLast(new HttpObjectAggregator(81920));
//                                // 业务处理器（提交到线程池）
//                                pipeline.addLast(new HttpBusinessHandler());
//                            }
//                        });
//
//                // 4. 绑定端口并启动
//                ChannelFuture f = b.bind(PORT).sync();
//                f.channel().closeFuture().sync();
//
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            } finally {
//                // 5. 释放资源
//                bossGroup.shutdownGracefully();
//                workerGroup.shutdownGracefully();
//            }
//        }).start();
//    }
//}